#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<set>
using namespace std;
ifstream ReadFile;
string Wenfa[1000][2];
set<string> false_Zhongjie;
set<string> Zhongjie;

//判断是否在非终结符号集中
bool Is_false_Zhongjie(string str){
    int status = 0;
    for(string s:false_Zhongjie){
        if(s.length()!=str.length())
            continue;
        status = 1;
        for(int i = 0;i<s.length();i++){
            if(s[i]!=str[i])
                status = 0;
        }
        if(status)
        break;

    }
      switch (status)
        {
        case 1: return true;
            break;
        
        default: return false;
            break;
    }

}
bool Is_Zhongjie(string str){
    int status = 0;
    for(string s:Zhongjie){
        if(s.length() !=str.length()){
            continue;
        }
        status = 1;
        for(int i = 0;i<s.length();i++){
            if(str[i]!=s[i]){
                status = 0;
            }
        }
        if(status)
        break;
    }
    switch (status)
    {
    case 1:return true;
        break;
    
    default: return false;
        break;
    }
}
int main(){
    //读取文件
    cin.unsetf(ios::skipws);
    ReadFile.open("wenfa.txt",ios::in);
    //default
    if(!ReadFile.is_open()){
        cout<<"read_file open error";
        return;
    }
        string str;
        int nums = 0;
        while(!ReadFile.eof()){
            //读取文件
            getline(ReadFile,str);
            int status = 1;
            for(int i = 0;i<str.length();i++){
                //将文法分割为二维
                //匹配到->
                if(str[i] == '-'&&str[i+1] == '>')
                {
                    status = 0;
                    i = i + 3;

                }
                switch (status)
                {
                case 1:if(str[i]!=' '){
                    Wenfa[i][0] +=str[i];

                }
                    break;
                case 0: Wenfa[i][1] +=str[i];
                default:
                    break;
                }
                status = 1;
                nums = nums +1;
            }
            //构建非终结符号集
            for(int i = 0;i<1000;i++)
            false_Zhongjie.insert(Wenfa[i][0]);

            //构建终结符号集
            for(int i = 0;i<1000;i++){
                istringstream str2(Wenfa[i][1]);
                string output;
                while(str2.good()){
                    getline(str2,output,' ');
                    if(!output.empty()&&(!Is_false_Zhongjie(output))){
                        Zhongjie.insert(output);
                    }
                }
            }

        }
        

    return;

}